package com.example.travelhelper.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.travelhelper.utils.fileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/file")

public class FileController {



    @Value("${imagepath}")
    private String filePath;
    @Value("${Getimagepath}")
    private String finderPath;
    @PostMapping("/uploadTest")

    public Map<String, Object> uploadForEditor(@RequestParam("wangeditor-uploaded-image") MultipartFile img) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {

            if (img==null||fileUtil.isEmpty(img)||img.getSize()>1024*1024*30) {
                map.put("msg", "上传失败");
                map.put("errno", 1); //错误代码，0表示没有错误
                return map;
            }
            String path = fileUtil.uploadFile(img, filePath,finderPath);

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("url",path);
            map.put("data", data);
            map.put("errno", 0);
            return map;
        }catch (Exception e){
            map.put("msg", "上传失败");
            map.put("errno", 1); //错误代码，0表示没有错误
            return map;
        }

    }
}
